﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using DAL;

namespace DLL.Teacher操作类DAL
{
    /// <summary>
    /// 将数据库中的IP查询出来
    /// </summary>
    public class SelectIpDAL
    {
        private SQLHelper sqlhelper = null;
        public SelectIpDAL()
        {
            sqlhelper = new SQLHelper();
        }

        #region 查询数据库中的所有IP数据-DataTable SelectIp(UserEntity userinfo)-杨光彩-2018-2-26 10:47:38
        /// <summary>
        /// 查询数据库中的所有IP数据
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public DataTable SelectIp(IPEntity ipEntity)
        {
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM IPEntity where ipPart=@ipPart ";
            SqlParameter[] paras = new SqlParameter[]{
                new  SqlParameter("@ipPart",ipEntity.ipPart)};
            dt = sqlhelper.ExecuteQuery(sql, paras, CommandType.Text);
            return dt;
        }
        #endregion


        #region 选择所有的IP数据--DataTable SelectAllIp()--杨光彩--2018-3-1 13:56:06
        /// <summary>
        /// 选择所有的IP数据
        /// </summary>
        /// <returns></returns>
        public DataTable SelectAllIp()
        {
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM IPEntity";
            dt = sqlhelper.ExecuteQuery(sql, CommandType.Text);
            return dt;
        }
        
        #endregion


        #region 通过ipname来查询IP--DataTable SelectOneIp(IPEntity ipEntity)--杨光彩--2018-3-1 18:04:08
        /// <summary>
        /// 通过ipname来查询IP
        /// </summary>
        /// <param name="ipEntity"></param>
        /// <returns></returns>
        public DataTable SelectOneIp(IPEntity ipEntity)
        {
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM IPEntity where IPName=@IPName ";
            SqlParameter[] paras = new SqlParameter[]{
                new  SqlParameter("@IPName",IPStaticEntity.IpName)};
            dt = sqlhelper.ExecuteQuery(sql, paras, CommandType.Text);
            return dt;
        }
        #endregion

        #region 更新ip-P-吕德彪-2018
        /// <summary>
        /// 更新IP-吕德彪-2018
        /// </summary>
        /// <param name="ipentity"></param>
        /// <returns></returns>
        public int UpdateIP(IPEntity ipentity)
        {
            string sql = "";
            int flag = 0;
            SqlParameter[] paras = null;

            sql = "update IPEntity set ipPart = @ipPart,ipStart = @ipStart,ipEnd = @ipEnd where ipID = @ipID and ipName = @ipName";
            paras = new SqlParameter[] {
                    new SqlParameter("@ipID", ipentity.ipID),
                    new SqlParameter("@ipName", ipentity.ipName),
                    new SqlParameter("@ipPart", ipentity.ipPart),
                    new SqlParameter("@ipStart", ipentity.ipStart),
                    new SqlParameter("@ipEnd", ipentity.ipEnd)
                    };
            flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);

            return flag;
        }
        #endregion

        #region 添加新的考场 -吕德彪-2018
        /// <summary>
        /// 添加新的考场
        /// </summary>
        /// <param name="ipentity"></param>
        /// <returns></returns>
        public int InsertIP(IPEntity ipentity)
        {
            string sql = "insert into IPEntity values (@ipName,@ipPart,@ipStart,@ipEnd)";
            SqlParameter[] paras = new SqlParameter[] {                   
                    new SqlParameter("@ipName", ipentity.ipName),
                    new SqlParameter("@ipPart", ipentity.ipPart),
                    new SqlParameter("@ipStart", ipentity.ipStart),
                    new SqlParameter("@ipEnd", ipentity.ipEnd)
                    };
            int flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);
            return flag;
        }
        #endregion

        #region 删除IP
        public int DeleteIP(string s)
        {
            string sql = "delete from IPEntity where IPName = @ipName";
            SqlParameter[] paras = new SqlParameter[] {
                    new SqlParameter("@ipName", s)                   
                    };
            int flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);
            return flag;
        }
        #endregion

    }
}
